﻿Imports Microsoft.Practices.Composite.Modularity
Imports Microsoft.Practices.Unity
Imports Infragistics.Windows.Ribbon
Imports Microsoft.Practices.Composite.Regions
Imports Hinshelwood.TFSStickyBuddy.Infrastructure
Imports Hinshelwood.TFSStickyBuddy.Modules.Interfaces
Imports Hinshelwood.TFSStickyBuddy.Modules.Controlers
Imports Hinshelwood.TFSStickyBuddy.Modules.Services
Imports Infragistics.Windows.Themes

Public Class ThemeModule
    Implements IModule

    Private ReadOnly m_container As IUnityContainer
    Private ReadOnly m_rmShell As IRegionManager
    Private ReadOnly m_NavigationService As INavigationService

    Public Sub New(ByVal container As IUnityContainer, ByVal regionManager As IRegionManager, ByVal NavigationService As INavigationService)
        m_container = container
        m_rmShell = regionManager
        m_NavigationService = NavigationService
    End Sub


    Public Sub Initialize() Implements Microsoft.Practices.Composite.Modularity.IModule.Initialize

        RegisterViewsAndServices()
        ' Get Controller
        Dim controller As IThemeController = m_container.Resolve(Of IThemeController)()
        ' Start Ball Roaling by adding views to the display
        m_NavigationService.EnsureBaseRegions()
        m_NavigationService.AddSeparatorTool()
        m_NavigationService.RegisterMenuTool(RegionNames.Theme_AppMenu_Themes, "Theme")
        m_NavigationService.AddLabelTool("Change the visual theme of the application.", RegionNames.Theme_AppMenu_Themes)
        For Each theme In controller.ThemeService.GetRegisteredThemes
            Dim newTool As New RadioButtonTool
            newTool.Caption = theme.Name
            newTool.LargeImage = GteImage(theme.ImageSource)
            newTool.IsChecked = theme Is controller.ThemeService.CurrentTheme
            newTool.Command = controller.ChangeThemeCommand
            AddHandler newTool.MouseEnter, AddressOf controller.OnThemeMouseEnter
            AddHandler newTool.MouseLeave, AddressOf controller.OnThemeMouseLeave
            newTool.CommandParameter = theme
            MenuToolBase.SetMenuItemDescription(newTool, theme.Description)
            m_NavigationService.AddTool(newTool, RegionNames.Theme_AppMenu_Themes)
        Next

        controller.TrySetTheme(controller.ThemeService.GetThemeByName(ThemeManager.ThemeNameOnyx))
    End Sub



    Private Function GteImage(ByVal uri As Uri) As ImageSource
        Dim stream As System.IO.Stream = Application.GetResourceStream(uri).Stream
        Dim bitmapDecoder As PngBitmapDecoder = New PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
        Return bitmapDecoder.Frames(0)
    End Function

    Protected Sub RegisterViewsAndServices()
        Dim Lipstick = New Infragistics.Windows.Themes.Internal.ThemesLipstickAssemblyResourceSetLoader
        Lipstick.RegisterResourceSets()
        Dim Leaf = New Infragistics.Windows.Themes.Internal.ThemesLeafAssemblyResourceSetLoader
        Leaf.RegisterResourceSets()
        '' Register Presentation Models
        'm_container.RegisterType(Of IAddRemoveServerPresentationModel, AddRemoveServerPresentationModel)()
        '' Register Presenters
        'm_container.RegisterType(Of IAddRemoveServerPresenter, AddRemoveServerPresenter)()
        '' Register Views
        'm_container.RegisterType(Of IAddRemoveServerView, AddRemoveServerView)()
        ' Register Services
        m_container.RegisterType(Of IThemeService, ThemeService)()
        ' Register Controlers
        m_container.RegisterType(Of IThemeController, ThemeController)()
    End Sub

End Class
